#ifndef VARIANCEIC_H
#define VARIANCEIC_H

#include "IndexCalculator.h"
#include "MeanIC.h"

class VarianceIC : public IndexCalculator<double, double>
{
private:
    MeanIC exp_squared; // 用于计算平方的期望
    MeanIC exp;         // 用于计算期望

public:
    explicit VarianceIC(int period) : IndexCalculator<double, double>(1), exp_squared(period), exp(period) {}

    void calculate(const double &new_data) override
    {
        // 更新平方的期望和期望
        double exp_of_squared_val = exp_squared.update(new_data * new_data);
        double squared_exp_val = exp.update(new_data);
        squared_exp_val *= squared_exp_val;

        // 计算方差
        this->result = exp_of_squared_val - squared_exp_val;
    }

    void reset() override
    {
        IndexCalculator<double, double>::reset();
        exp_squared.reset();
        exp.reset();
    }
};

#endif // VARIANCEIC_H